<?php
/**
 * bmi 体重指数计算
 */
$this->title = 'My Yii Application';
$css = <<<CSS
.sl{width:50px;display: inline-block;padding:6px 5px}
.sm{display: inline-block;width:10rem}
#result{margin-top: 10px}
CSS;

$this->registerCss($css);

$js = <<<JS

function getElem(str){
	return document.querySelector(str);
}
var smt = getElem("#smt");
var cur_href = decodeURI(location.href);
if (cur_href.indexOf('#') > -1) {
    var _http = cur_href.substring(0, cur_href.indexOf('#'));
}
var standard = {
    variables: {
        height: 0,
        weight: 0,
        gender: 0,
        age: 0,
        sport: 0,
        idealWeight: 0,
        bodyExponent: 0,
        bodyStatus: "",
        pulseRate: [0, 0],
        pulsePressure: [30, 40],
        beeStandard: 0,
        energyStandard: 0,
        gain: 0,
        loss: 0,
        popObj: null,
        tar: null,
        idx: null,
        tyId: null
    },
    testMe: function (idx) {
        this.variables.height = document.getElementById("Jheight").value;
        this.variables.weight = document.getElementById("Jweight").value;
        this.variables.age = document.getElementById("Jage").value;
        this.variables.gender = document.getElementById("Jgender").value;
        this.variables.sport = document.getElementById("Jsport").value;
        if (this.checkForm()) {
            // this.comput();
            window.location.href = '?s=' + (this.variables.gender == '0' ? 'g' : 'b') + '&' + this.variables.height + '_' + this.variables.weight + '_' + this.variables.age + '_' + this.variables.sport;
        } else {
            openDialog("请输入正确且真实的数值!");
        }
    },
    checkForm: function () {
        if (isNaN(this.variables.height) || isNaN(this.variables.weight) || isNaN(this.variables.gender) || (Number(this.variables.height) < 1 || Number(this.variables.height) > 400) || (Number(this.variables.weight) < 1 || Number(this.variables.weight) > 300) || isNaN(this.variables.age) || (Number(this.variables.age) < 1 || Number(this.variables.age) > 110) || isNaN(this.variables.sport)) {
            return false;
        }
        return true;
    },
    calculat: function (x) {
        return (Math.round(x * 100) / 100);
    },
    comput: function () {
        this.variables.bodyExponent = Math.round(this.variables.weight * 10000 / (this.variables.height * this.variables.height));
        this.variables.idealWeight = ((parseInt(this.variables.gender) == 1) ? (Math.round(50 + (2.3 * (this.variables.height - 152)) / 2.54)) : (Math.round(45.5 + (2.3 * (this.variables.height - 152)) / 2.54)));
        if (this.variables.bodyExponent > 40) {
            this.variables.bodyStatus = "您体型严重肥胖";
        } else if (this.variables.bodyExponent > 30) {
            this.variables.bodyStatus = "您体型肥胖";
        } else if (this.variables.bodyExponent > 27) {
            this.variables.bodyStatus = "您体型偏胖";
        } else if (this.variables.bodyExponent > 22) {
            this.variables.bodyStatus = "您体型稍胖";
        } else if (this.variables.bodyExponent >= 21) {
            this.variables.bodyStatus = "您体型正常";
        } else if (this.variables.bodyExponent >= 18) {
            this.variables.bodyStatus = "您体型偏瘦";
        } else if (this.variables.bodyExponent >= 16) {
            this.variables.bodyStatus = "您体型较瘦";
        } else {
            this.variables.bodyStatus = "您体型严重消瘦";
        }
        // heart about
        if (this.variables.gender.toString() == "1") {
            if (this.variables.age >= 0 && this.variables.age <= 3) {
                this.variables.beeStandard = this.calculat(60.9 * this.variables.weight - 54);
            } else if (this.variables.age > 3 && this.variables.age <= 10) {
                this.variables.beeStandard = this.calculat(22.7 * this.variables.weight - 495);
            } else if (this.variables.age > 10 && this.variables.age <= 18) {
                this.variables.beeStandard = this.calculat(17.5 * this.variables.weight + 651);
            } else if (this.variables.age > 18 && this.variables.age <= 30) {
                this.variables.beeStandard = this.calculat(15.3 * this.variables.weight + 679);
            } else if (this.variables.age > 30 && this.variables.age <= 60) {
                this.variables.beeStandard = this.calculat(11.6 * this.variables.weight + 879);
            } else if (this.variables.age > 60) {
                this.variables.beeStandard = this.calculat(13.5 * this.variables.weight + 487);
            }
        } else {
            if (this.variables.age >= 0 && this.variables.age <= 3) {
                this.variables.beeStandard = this.calculat(61 * this.variables.weight - 51);
            } else if (this.variables.age > 3 && this.variables.age <= 10) {
                this.variables.beeStandard = this.calculat(22.5 * this.variables.weight + 499);
            } else if (this.variables.age > 10 && this.variables.age <= 18) {
                this.variables.beeStandard = this.calculat(12.2 * this.variables.weight + 746);
            } else if (this.variables.age > 18 && this.variables.age <= 30) {
                this.variables.beeStandard = this.calculat(14.7 * this.variables.weight + 496);
            } else if (this.variables.age > 30 && this.variables.age <= 60) {
                this.variables.beeStandard = this.calculat(8.7 * this.variables.weight + 829);
            } else if (this.variables.age > 60) {
                this.variables.beeStandard = this.calculat(10.5 * this.variables.weight + 596);
            }
        }
        if (this.variables.gender.toString() == "1") {
            switch (this.variables.sport.toString()) {
            case "0":
                this.variables.energyStandard = this.calculat(this.variables.beeStandard * 1.55);
                var low = this.variables.beeStandard * 1.55 + 500;
                var high = this.variables.beeStandard * 1.55 + 1000;
                this.variables.gain = this.calculat(low) + " ~ " + this.calculat(high);
                var low1 = this.variables.beeStandard * 1.55 - 1000;
                var high1 = this.variables.beeStandard * 1.55 - 500;
                this.variables.loss = this.calculat(low1) + " ~ " + this.calculat(high1);
                break;
            case "1":
                this.variables.energyStandard = this.calculat(this.variables.beeStandard * 1.78);
                var low = this.variables.beeStandard * 1.78 + 500;
                var high = this.variables.beeStandard * 1.78 + 1000;
                this.variables.gain = this.calculat(low) + " ~ " + this.calculat(high);
                var low1 = this.variables.beeStandard * 1.78 - 1000;
                var high1 = this.variables.beeStandard * 1.78 - 500;
                this.variables.loss = this.calculat(low1) + " ~ " + this.calculat(high1);
                break;
            case "2":
                this.variables.energyStandard = this.calculat(this.variables.beeStandard * 2.10);
                var low = this.variables.beeStandard * 2.10 + 500;
                var high = this.variables.beeStandard * 2.10 + 1000;
                this.variables.gain = this.calculat(low) + " ~ " + this.calculat(high);
                var low1 = this.variables.beeStandard * 2.10 - 1000;
                var high1 = this.variables.beeStandard * 2.10 - 500;
                this.variables.loss = this.calculat(low1) + " ~ " + this.calculat(high1);
                break;
            }
        } else {
            switch (this.variables.sport.toString()) {
            case "0":
                this.variables.energyStandard = this.calculat(this.variables.beeStandard * 1.56);
                var low = this.variables.beeStandard * 1.56 + 500;
                var high = this.variables.beeStandard * 1.56 + 1000;
                this.variables.gain = this.calculat(low) + " ~ " + this.calculat(high);
                var low1 = this.variables.beeStandard * 1.56 - 1000;
                var high1 = this.variables.beeStandard * 1.56 - 500;
                this.variables.loss = this.calculat(low1) + " ~ " + this.calculat(high1);
                break;
            case "1":
                this.variables.energyStandard = this.calculat(this.variables.beeStandard * 1.64);
                var low = this.variables.beeStandard * 1.64 + 500;
                var high = this.variables.beeStandard * 1.64 + 1000;
                this.variables.gain = this.calculat(low) + " ~ " + this.calculat(high);
                var low1 = this.variables.beeStandard * 1.64 - 1000;
                var high1 = this.variables.beeStandard * 1.64 - 500;
                this.variables.loss = this.calculat(low1) + " ~ " + this.calculat(high1);
                break;
            case "2":
                this.variables.energyStandard = this.calculat(this.variables.beeStandard * 1.82);
                var low = this.variables.beeStandard * 1.82 + 500;
                var high = this.variables.beeStandard * 1.82 + 1000;
                this.variables.gain = this.calculat(low) + " ~ " + this.calculat(high);
                var low1 = this.variables.beeStandard * 1.82 - 1000;
                var high1 = this.variables.beeStandard * 1.82 - 500;
                this.variables.loss = this.calculat(low1) + " ~ " + this.calculat(high1);
                break;
            }
        }
    },
    fill: function () {
        document.getElementById("idealWeightPanel").innerHTML = this.variables.idealWeight + "公斤";
        document.getElementById("bodyExponentPanel").innerHTML = this.variables.bodyExponent + "(21-22为最佳)";
        document.getElementById("bodyStatusPanel").innerHTML = this.variables.bodyStatus;
        document.getElementById("beePanel").innerHTML = this.variables.beeStandard + "卡路里/天";
        document.getElementById("energyPanel").innerHTML = this.variables.energyStandard + "卡路里/天";
        document.getElementById("gainPanel").innerHTML = this.variables.gain + "卡路里/天";
        document.getElementById("lossPanel").innerHTML = this.variables.loss + "卡路里/天";
    }
};
if (window.location.search && window.location.search.indexOf("?s=") != -1) {
    var a = window.location.search.substr(3).split("&"),
    d = a[1].split("_");
    a[0] == 'b' ? (document.getElementById("Jgender").value = '1') : (document.getElementById("Jgender").value = '0');
    document.getElementById("Jheight").value = d[0];
    document.getElementById("Jweight").value = d[1];
    document.getElementById("Jage").value = d[2];
    document.getElementById("Jsport").value = d[3];
    standard.variables.height = document.getElementById("Jheight").value;
    standard.variables.weight = document.getElementById("Jweight").value;
    standard.variables.age = document.getElementById("Jage").value;
    standard.variables.sport = document.getElementById("Jsport").value;
    standard.variables.gender = a[0] == 'b' ? '1' : '0';
    if (standard.checkForm()) {
        standard.comput();
        standard.fill();
        getElem("#result").style.display='block';
    } else {
        openDialog("请输入正确且真实的数值!");
    }
} 
    smt.onclick = function () {
        if (!!_http) {
            window.location = _http + '?s=' + (standard.variables.gender == '0' ? 'g' : 'b') + '&' + standard.variables.height + '_' + standard.variables.weight + '_' + standard.variables.age + '_' + standard.variables.sport;
        } else {
            standard.testMe()
        }
    }


JS;
$this->registerJs($js);

?>

<div class="site-index">
    <div class="body-content">
        <h3>bmi 体重指数计算</div>
    <hr/>
    <form >
        <div class="form-group  text-center col-md-12">
            <div class="input-group col-lg-4">
                <span class="input-group-addon">您的性别</span>
                <select class="form-control" id="Jgender">
                    <option value="0">女</option>
                    <option value="1">男</option>
                </select>

            </div>
        </div>

        <div class="form-group  text-center col-md-12">
            <div class="input-group  col-lg-4">
                <span class="input-group-addon">您的身高</span><input id="Jheight" type="number" class="form-control sm" placeholder="请输入身高"/><span
                    class="input-group-addon">厘米</span>
            </div>
        </div>
        <div class="form-group  text-center col-md-12">
            <div class="input-group col-lg-4">
                <span class="input-group-addon">您的体重</span><input id="Jweight" type="number" class="form-control" placeholder="请输入体重"/><span
                        class="input-group-addon">千克</span>
            </div>
        </div>
        <div class="form-group  text-center col-md-12">
            <div class="input-group col-lg-4">
                <span class="input-group-addon">您的年龄</span><input id="Jage" type="number" class="form-control" placeholder="请输入体重"/><span
                        class="input-group-addon">周岁</span>
            </div>
        </div>



        <div class="form-group  text-center col-md-12">
            <div class="input-group col-lg-4">
                <span class="input-group-addon">日运动量</span>
                <select class="form-control" id="Jsport">
                    <option value="0">轻度</option>
                    <option value="1">中度</option>
                    <option value="2">重度</option>
                </select>

            </div>
        </div>



        <div class="col-lg-4 text-center" style="margin-top: 4px">
            <a id="smt" class="btn btn-primary col-lg-4 col-xs-6" style="float: initial !important;">
                <i class="icon-chevron-down icon-white"></i>计算</a>
        </div>

        <div class="col-xs-12 col-lg-12 col-md-12  moreheight " id="result" style="height: 4rem;text-height:4rem;display: none">
            <div id="showDiv">
                <table class="showDiv-info-table">
                    <tbody>
                    <tr>
                        <td class="showDiv-title">测试结果</td>
                    </tr>
                    <tr valign="top">
                        <td><p><em class="red" id="bodyStatusPanel"></em></p>
                            <p>理想体重：<em id="idealWeightPanel"></em></p>
                            <p>身体质量指数：<em id="bodyExponentPanel"></em></p>
                            <p>基础代谢率：<em id="beePanel"></em></p>
                            <p>每天需要热量：<em id="energyPanel"></em></p>
                            <p>如需增重：<em id="gainPanel"></em></p>
                            <p>如需减重：<em id="lossPanel"></em></p></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </form>










<div class="help-block col-lg-10">

</div>

</div>



